﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CompanyManagementDLL;
using CompanyManagementDLL.DLL;
using CompanyManagementDLL.MetaData;
using CompanyManagement.Models;
using log4net;

namespace CompanyManagement.Controllers
{
    public class ChangePasswordController : Controller
    {
        //
        // GET: /ChangePassword/
        UserDLL dllUser = new UserDLL();

        private static readonly ILog Log = LogManager.GetLogger(typeof(ChangePasswordController));
        public ActionResult Index()
        {
            if (!Utility.checkCookie())
            {
                return RedirectToAction("Index", "Login");
            }

            Utility.setCookiValue();
            return View();
        }

        [HttpPost]
        public ActionResult Index(ChangePassword userdata)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (dllUser.ChangePassword(Utility.getUserID(), userdata.OldPassword, userdata.NewPassword))
                    {
                        TempData["PasswordChange"] = "Your password has been changed.";
                    }
                    else
                    {
                        ModelState.AddModelError("", "The current password is incorrect is invalid.");
                        return View(userdata);
                    }
                }
                else
                {
                    return View(userdata);
                }
            }
            catch (Exception ex)
            {

                throw;
            }
            return View();
        }

    }
}
